package com.hex.liyue.lookingdatemodule.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.config.Constant;
import com.hex.liyue.commonmodule.manager.UserManager;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hex.liyue.commonmodule.view.IosChoiceDialog;
import com.hex.liyue.commonmodule.view.recycleview.YRecycleview;
import com.hex.liyue.giftmodule.activity.MyGiftsActivity;
import com.hex.liyue.lookingdatemodule.adapter.AdapterLooking;
import com.hex.liyue.lookingdatemodule.bean.LookingListResultBean;
import com.hex.liyue.lookingdatemodule.presenter.ILookingListView;
import com.hex.liyue.lookingdatemodule.presenter.LookingListPresenter;
import com.hex.liyue.messagemodule.activity.ChatActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by WJ on 2016/6/14.
 * 我应约约会列表界面
 */
public class LookingListActivity extends BaseActivity implements AdapterLooking.OnItemClickListener, ILookingListView, YRecycleview.RefreshAndLoadMoreListener {
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.rv_looking_list)
    YRecycleview rvLookingList;
    @Bind(R.id.empty_apply)
    LinearLayout emptyApply;

    private LookingListPresenter lookingListPresenter;
    private List<LookingListResultBean.DataBean> list = new ArrayList<>();
    private AdapterLooking adapterLooking;
    private int page = 1;
    private int pagenum = 10;

    @Override
    protected void bindPresenter() {
        lookingListPresenter = new LookingListPresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        lookingListPresenter.getApplyList(page, pagenum);
        rvLookingList.setRefreshAndLoadMoreListener(this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_looking_list;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        tvToobarTitle.setText("我应约的约会");
        ivToobarBack.setVisibility(View.VISIBLE);
    }

    @OnClick({R.id.iv_toobar_back})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
        }
    }

    @Override
    public void onItemClickListener(String appointment_id) {
        Intent intent = new Intent(this, InSomeDetailsActivity.class);
        intent.putExtra("appointment_id", appointment_id);
        startActivity(intent);
    }

    @Override
    public void onLockGiftListener() {
        Intent intent = new Intent(this, MyGiftsActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onChatListener(String fromUid, String fromAvatar, String fromName) {
//        EMMessage message= EMMessage.createTxtSendMessage(String.valueOf(LiYueApplication.getInstance()), String.valueOf(UserManager.getUserId()));
//        message.setAttribute("fromName", fromName);
//        L.e("LookfromName", fromName);
//        message.setAttribute("fromUid", fromUid);
//        message.setAttribute("fromAvatar",fromAvatar);
//        message.setAttribute("toAvatar", UserManager.getUserAvator());
//        message.setAttribute("toUid", UserManager.getUserId());
//        message.setAttribute("toName",  UserManager.getNickName());
//        EaseChatFragment easeChatFragment=new EaseChatFragment();
//        easeChatFragment.sendMessage(message);

        Intent intent = new Intent(this, ChatActivity.class);
        intent.putExtra(Constant.EXTRA_USER_ID, fromUid);
        intent.putExtra("fromAvatar", fromAvatar);
        intent.putExtra("fromName", fromName);
        intent.putExtra("toUid", UserManager.getUserId());
        intent.putExtra("toAvatar", UserManager.getUserAvator());
        intent.putExtra("toName", UserManager.getNickName());
        intent.putExtra("flag", "flag");
        startActivity(intent);
    }

    @Override
    public void onConfimSuccess(final String toUid, final String appointmentId) {
        IosChoiceDialog iosChoiceDialog = new IosChoiceDialog(this, "系统提示", "确认完成本次约会吗？");
        iosChoiceDialog.setOnButtonClickListener(new IosChoiceDialog.OnButtonClickListener() {
            @Override
            public void onOkClickListener() {
                lookingListPresenter.successAppointment(appointmentId, toUid);
            }

            @Override
            public void onCancelClickListener() {

            }
        });
        iosChoiceDialog.show();
    }

    @Override
    public void showLod(String msg) {
        showLoad(this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(this, msg);
    }



    @Override
    public void setAdapter(LookingListResultBean lookingListResultBean) {
        if (page == 1) {
            list.clear();
        }
        if (lookingListResultBean != null) {
            if (lookingListResultBean.data.size() == 0 && page > 1) {
                page--;
            }
            for (LookingListResultBean.DataBean dataBean : lookingListResultBean.data) {
                list.add(dataBean);
            }
        }

        if (adapterLooking == null) {
            adapterLooking = new AdapterLooking(list);
            adapterLooking.setOnItemClickListener(this);
            rvLookingList.setLayoutManager(new LinearLayoutManager(this));
            rvLookingList.setEmptyView(emptyApply);
            rvLookingList.setAdapter(adapterLooking);
        }
        adapterLooking.notifyDataSetChanged();
        rvLookingList.reSetStatus();
        L.d("WJ", page + "=size=" + lookingListResultBean.data.size());
    }

    @Override
    public void onRefresh() {
        page = 1;
        lookingListPresenter.getApplyList(page, pagenum);
    }

    @Override
    public void onLoadMore() {
        page++;
        lookingListPresenter.getApplyList(page, pagenum);
    }


}
